package cn.tutu.blockchain.controller;

import javax.validation.Valid;

import org.hibernate.validator.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;

import cn.tutu.blockchain.common.MsgObj;
import cn.tutu.blockchain.common.Page;
import cn.tutu.blockchain.exchange.entity.Exchange;
import cn.tutu.blockchain.exchange.service.ExchangeService;

@RestController
@RequestMapping("exchange")
@Validated
public class ExchangeController {

	private static Logger LOGGER = LoggerFactory.getLogger(SysuserController.class);
	@Autowired
	private ExchangeService exchangeService;

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public MsgObj create(@Valid Exchange exchange) {
		LOGGER.info("========创建兑换记录:{}======", exchange.toString());
		exchangeService.create(exchange);
		return MsgObj.buildSuccessMsg();
	}

	@RequestMapping(value = "/modify")
	public MsgObj modify(@NotBlank(message = "状态不能为空!") String id, @NotBlank(message = "状态不能为空!") String status) {
		LOGGER.info("========修改兑换记录:{}======", status);
		Exchange exchange = new Exchange();
		exchange.setId(id);
		exchange.setStatus(status);
		exchangeService.modifyStatus(exchange);
		return MsgObj.buildSuccessMsg();
	}

	@RequestMapping("/getGridByUser")
	public MsgObj getGridByUser(@NotBlank(message = "用户ID不能为空!") String userId, Page page) {
		PageInfo<Exchange> byModel = exchangeService.getByUserId(userId, page);
		return MsgObj.buildSuccessObj(byModel.getList());
	}

}
